<?php
Class News
{
    public static function getNewslist1($page, $dataPerpage, $lang, $isAdmin) {
        
        $status = $isAdmin?"'P','U'":"'P'";//ปรับสเตตัส ให้เห็นข้อมูลตามสิทธ์
        $sql = "SELECT *
            FROM news
            WHERE Status IN ({$status}) AND DeleteDate IS NULL 
            ORDER BY CreateDate DESC, NewsID DESC";
        $connection=Yii::app()->db; 
        $command=$connection->createCommand($sql);
        $maxPage =  ceil($command->execute()/$dataPerpage);
        
        $langUpper = strtoupper($lang);
        $startRow = $dataPerpage*($page-1);
        $sql = "SELECT *, Title{$langUpper} AS NewsTitle, Info{$langUpper} AS NewsInfo
            FROM news
            WHERE Status IN ({$status}) AND DeleteDate IS NULL 
            ORDER BY CreateDate DESC, NewsID DESC
            LIMIT {$startRow},{$dataPerpage}";
        $connection=Yii::app()->db; 
        $command=$connection->createCommand($sql);
        $data =  $command->queryAll();
        return array(
            'MaxPage' => $maxPage,
            'data' => $data
        );
    }
    
    public static function getNewsDetail($newsID, $lang){
        $langUpper = strtoupper($lang);
        $sql="SELECT *, Title{$langUpper} AS NewsTitle, Info{$langUpper} AS NewsInfo
            FROM news WHERE NewsID = :ID;";
        $connection = Yii::app()->db;
        $command = $connection->createCommand($sql);
        $command->bindParam(":ID",$newsID);
        return $command->queryRow();
    }

    public static function getNewslist($page, $dataPerpage){
        $startRow = $dataPerpage*($page-1);
        $sql="SELECT * FROM news WHERE DeleteDate Is Null LIMIT :startRow,:dataPerpage;";
        $connection=Yii::app()->db; 
        $command=$connection->createCommand($sql);
        $command->bindParam(':startRow', $startRow);
        $command->bindParam(':dataPerpage', $dataPerpage);
        return $command->queryAll();
    }
    
    public static function ChangeState($Id, $state){
        $state = ($state == "P")?"U":"P";
        $sql = "UPDATE news SET Status = :state WHERE id = :Id";
        $connection = Yii::app()->db;
        $command = $connection->createCommand($sql);
        $command->bindParam(':state', $state);
        $command->bindParam(':Id', $Id);
        $command->query();
    }
    
    Public static function getNewsEdit($Nid){
        $sql = "SELECT * FROM news WHERE NewsID = :Nid;";
        $connection=Yii::app()->db; 
        $command=$connection->createCommand($sql);
        $command->bindParam(':Nid', $Nid);
        return $command->queryAll();
    }
}
?>
